package caiwei.android.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import caiwei.android.R;
import caiwei.android.service.ServiceMyService;
import caiwei.android.service.ServiceMyService.MyBinder;

/**
 * 基础知识之三，service的演示
 * 
 * @author caiwei
 * @date2012-3-19
 * @version 1.0
 */
public class ActivityServiceDemo extends ActivityGridBaseActivityFirst
{
	private Intent mIntent;
	private ServiceConnection conn = new ServiceConnection()
	{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service)
		{
			MyBinder _Mybinder = (MyBinder) service;
			_Mybinder.playMusicForOut();
		}

		@Override
		public void onServiceDisconnected(ComponentName name)
		{
		}
	};

	@Override
	protected void initVariable()
	{
		mTextViewText = "ActivicyServiceDemo";
	}

	@Override
	protected void initView()
	{
		super.initView();
		// mSelfButton=(Button)findViewById(R.id.btnSelf);
		mSelfButton.setText(R.string.startservice);
		mSelfButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mIntent = new Intent(ActivityServiceDemo.this,
						ServiceMyService.class);
				startService(mIntent);
				showMsg("服务开始");
			}
		});
		mFristToSecondButton.setText(R.string.stopservice);
		mFristToSecondButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mIntent = new Intent(ActivityServiceDemo.this,
						ServiceMyService.class);
				stopService(mIntent);
				showMsg("服务停止");
			}
		});
		mFristToThirdButton.setText(R.string.bindservice);
		mFristToThirdButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				mIntent = new Intent(ActivityServiceDemo.this,
						ServiceMyService.class);
				bindService(mIntent, conn, BIND_AUTO_CREATE);
				showMsg("服务绑定");
			}
		});
		mFristToFourtButton.setText(R.string.unbindervice);
		mFristToFourtButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mIntent = new Intent(ActivityServiceDemo.this,
						ServiceMyService.class);
				unbindService(conn);
				showMsg("服务解除绑定");
			}
		});
	}
}
